﻿using B.S.BasicData.Api.Write.Application.Command.User;
using B.S.BasicData.Domain.Entity;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infastructure.Interface;
using MediatR;

namespace B.S.BasicData.Api.Write.Application.Handler.User
{
    public class DeleteUserEmpHandler : IRequestHandler<DeleteUserEmpCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<UserRoleModel> _userRoleRepository;

        public DeleteUserEmpHandler(IBaseRepository<UserRoleModel> userRoleRepository)
        {
            _userRoleRepository = userRoleRepository;
        }
        /// <summary>
        /// 删除单个与员工
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<int>> Handle(DeleteUserEmpCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();

             var userrole = _userRoleRepository.GetAll().Where(x => x.UserId == request.UserId && x.RoleId == request.RoleId).ToList();
            if (userrole != null)
            {
                _userRoleRepository.DeleteRange(userrole);
            }
            result.Code = StatusEnum.OK;
            result.Data = 1;
            result.Message = "删除成功";
            return Task.FromResult(result);
        }
    }
}
